package cn.fz.service.impl;

import cn.fz.dao.DeptDao;
import cn.fz.pojo.Dept;
import cn.fz.service.DeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;

//@Primary //这个bean的优先级高
@Service
public class DeptServiceImpl2 implements DeptService {
    @Autowired
    private DeptDao deptDao;

    @Override
    public List<Dept> list() throws Exception {
        System.out.println("DeptServiceImpl2 ..... 22222222222222222");
        //1. 调用dao获取数据
        List<String> lines = deptDao.list();
        //2. 解析文本中的数据, 封装为对象 ; 多个对象封装到集合中
        return lines.stream().map(line -> {
            String[] parts = line.split(",");
            int id = Integer.parseInt(parts[0]);
            String name = parts[1];
            LocalDateTime updateTime = LocalDateTime.parse(parts[2], DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
            return new Dept(id, name, updateTime);
        }).toList();
    }
}
